package com.crishne.anwo.service.impl;
 
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;

import com.crishne.anwo.dao.UserDao;
import com.crishne.anwo.model.Menu;
import com.crishne.anwo.model.User;
import com.crishne.anwo.service.UserService;
import com.crishne.anwo.utill.PaginationUtil;
 
 
@Service
@Transactional
public class UserServiceImpl implements UserService{
 
    @Autowired
    private UserDao userDAO;
     
    @Override
    public void addUser(User user) {
        userDAO.addUser(user);
    }
    
    @Override
    public void updateUser(User user) {
        userDAO.updateUser(user);
    }
    
    @Override
    public void deleteUser(String ids) {
    	String[] idArray = ids.split(",");
    	for(int i=0;i<idArray.length;i++){
    		userDAO.deleteUser(idArray[i]);
    	}
    }
    
    @Override
    public User getUserById(String stUserId){
    	return userDAO.selectUserById(stUserId);
    }
    
    @Override
    public User getUserByName(String stUserName){
    	return userDAO.getUserByName(stUserName);
    }
    
    @Override
    public List<User> findUsers(String searchCondition) {
        return userDAO.findUsers(searchCondition);
    }
    
    @Override
    public PaginationUtil<User> searchUsers(PaginationUtil<User> pageConditions){
			//当前页
	        Integer currentPage = pageConditions.getCurrentPage()==null?1:pageConditions.getCurrentPage();
	        //每页数据数
	        Integer pageSize = pageConditions.getPageSize()==null?10:pageConditions.getPageSize();
	        
	    	pageConditions.setCurrentPage(currentPage);
	        pageConditions.setPageSize(pageSize);
	        //pageConditions.setTotalCount(userList);
	        List<User> userList = userDAO.searchUsers(pageConditions);
	        int totalCount = userDAO.getTotalResult(pageConditions);
	        pageConditions.setDataList(userList);
	        pageConditions.setTotalCount(totalCount);
	        if(totalCount == 0){
	        	pageConditions.setTotalPage(1);
	        }else if(totalCount%pageSize == 0){
	        	pageConditions.setTotalPage(totalCount/pageSize);
	        }else{
	        	pageConditions.setTotalPage(totalCount/pageSize+1);
	        }
	    	return pageConditions;
    }
    
    @Override
    public User checkUser(String stUserName,String stUserId){
    	return null;
    }
    
    
    @SuppressWarnings("rawtypes")
	@Override
    public List<HashMap> findRecords(String sql){
    	return userDAO.findRecords(sql);
    }
}